//
//  ClassViewController.m
//  A段项目音乐播放
//
//  Created by 太阳 on 15/5/25.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "ClassViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "SingerModel.h"
#import "singerActionView.h"
#import "ClassSongConntroller.h"
#import "PendulumView.h"
@interface ClassViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain)PendulumView *pen;
@end

@implementation ClassViewController
- (void)loadView
{
    [super loadView];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self builtInterface];
    [self requestnet];
//    [self.view setBackgroundColor:[UIColor colorWithRed:0.15 green:0.7 blue:0.6 alpha:1]];
    UIColor *ballColor = [UIColor colorWithRed:0.47 green:0.60 blue:0.89 alpha:1];
    self.pen = [[PendulumView alloc] initWithFrame:self.view.bounds ballColor:ballColor];
    [self.view addSubview:self.pen];
}
- (void)builtInterface
{
    //建界面
    self.myscrollview = [[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)] autorelease];
    self.myscrollview.delegate = self;
    [self.view addSubview:self.myscrollview];
//    [self.myscrollview setBackgroundColor:[UIColor colorWithWhite:0.2 alpha:0.2]];
    
    [self putscroll];
}

//铺界面
- (void)putscroll
{
    CGFloat kongge = [UIScreen mainScreen].bounds.size.width * 0.02;
    CGFloat width = [UIScreen mainScreen].bounds.size.width / 3.2769;
    
    int i = 0;
    for (SingerModel *model in self.modelarray) {
        
        singerActionView *view = [[[singerActionView alloc] initWithFrame:CGRectMake(kongge+ i % 3 * (width + kongge), i / 3 * (width + kongge + 20), width, width + kongge * 2 + 20)] autorelease];
//        [view setBackgroundColor:[UIColor greenColor]];
        [self.view addSubview:view];
            [view clickedWithBlock:^(NSInteger temp) {
                ClassSongConntroller *song = [[[ClassSongConntroller alloc]init] autorelease];
                song.mymodel = [self.modelarray objectAtIndex:i];
                
                [self.navigationController pushViewController:song animated:YES];
            }];
        UIImageView *myimageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, width)];
        [myimageview sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", model.small_pic_url]] placeholderImage:nil options:SDWebImageRetryFailed];
        [view addSubview:myimageview];
        [self.myscrollview addSubview:view];
        
        UILabel *mylabel = [[[UILabel alloc]initWithFrame:CGRectMake(kongge + i % 3 * (width + kongge), i / 3 * (width + kongge + 20) + width, width, 30)] autorelease];
//        [mylabel setBackgroundColor:[UIColor greenColor]];
        mylabel.text = model.songlist_name;
        mylabel.textAlignment = NSTextAlignmentCenter;
        mylabel.textColor = [UIColor colorWithWhite:0.2 alpha:0.8];
        mylabel.font = [UIFont systemFontOfSize:15];
        [self.myscrollview addSubview:mylabel];
        
        i++;
        
    }
    
    self.myscrollview.contentSize = CGSizeMake(0, self.modelarray.count * width / 2 - [UIScreen mainScreen].bounds.size.width - 100);
    [self.pen removeFromSuperview];
    
}

- (void)requestnet
{
    NSString *str = [NSString stringWithFormat:@"http://fm.api.ttpod.com/channellist?image_type=240_200&app=ttpod&v=v7.9.1.2015050518&uid=&mid=iPhone5S&f=f320&s=s310&imsi=&hid=&splus=8.3&active=1&net=2&openudid=826886cd55c251bacd39b2d6523c12b126f3c590&idfa=27D84D88-B1C6-4173-B7C5-EE78CD26E7FB&utdid=VVrl99FV5JUDAA1STsAtAkrx&alf=201200&bundle_id=com.ttpod.music"];
    NSString *address = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manger GET:address parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSData *data = [NSData dataWithData:responseObject];
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *array = [dic objectForKey:@"data"];
        self.modelarray = [NSMutableArray array];
        for (NSDictionary *dicc in array) {
            SingerModel *model = [[[SingerModel alloc]init] autorelease];
            [model setValuesForKeysWithDictionary:dicc];
            [self.modelarray addObject:model];//存满了model数据
        }
        [self putscroll];

        
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"%@", error);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
